package com.ego.dubbo.service.impl;

import com.ego.dubbo.service.TBContentDubboService;
import com.ego.mapper.TbContentMapper;
import com.ego.pojo.TbContent;
import com.ego.pojo.TbContentExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@DubboService
public class TbContentDubboServiceImpl implements TBContentDubboService {

    @Autowired
    private TbContentMapper tbContentMapper;

    @Override
    public PageInfo<TbContent> selectTbContentPageByCategoryId(long categoryId, int page, int size) {


        PageHelper.startPage(page,size);

        TbContentExample tbContentExample = new TbContentExample();

        if(categoryId > 0){
            tbContentExample.createCriteria().andCategoryIdEqualTo(categoryId);
        }
        List<TbContent> tbContents =  tbContentMapper.selectByExampleWithBLOBs(tbContentExample);

        PageInfo<TbContent> pageInfo = new PageInfo<>(tbContents);

        return pageInfo;
    }

    @Override
    public int insert(TbContent tbContent) {
        return tbContentMapper.insert(tbContent);
    }

    @Override
    public int update(TbContent tbContent) {
        return tbContentMapper.updateByPrimaryKeySelective(tbContent);
    }

    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(List<Long> ids) {

        TbContentExample tbContentExample = new TbContentExample();
        tbContentExample.createCriteria().andIdIn(ids);

        int rows = tbContentMapper.deleteByExample(tbContentExample);

        if(ids.size() == rows){
            return 1;
        }
        return 0;
    }

    @Override
    public List<TbContent> selectTbContentsByCategoryId(long categoryId) {

        TbContentExample tbContentExample = new TbContentExample();
        tbContentExample.createCriteria().andCategoryIdEqualTo(categoryId);

        List<TbContent> tbContents = tbContentMapper.selectByExample(tbContentExample);
        return tbContents;
    }

    @Override
    public List<TbContent> selectByIds(List<Long> ids) {
        TbContentExample tbContentExample = new TbContentExample();
        tbContentExample.createCriteria().andIdIn(ids);
        List<TbContent> tbContents = tbContentMapper.selectByExample(tbContentExample);
        return tbContents;
    }
}
